package org.ceteca.explica.client.gui.common;

import java.awt.Dimension;

import org.apache.log4j.Logger;
import org.ceteca.explica.client.ClientContext;
import org.ceteca.explica.client.gui.ExplicaGUIBuilder;
import org.ceteca.explica.core.partners.AddressOT;
import org.ceteca.explica.core.partners.InterfacePartners;
import org.ceteca.explica.core.util.GoogleMap;
import org.ceteca.explica.core.util.OperationResult;
import org.ceteca.javaglade.JavaGladeException;
import org.ceteca.javaglade.handlers.GenericHandler;
import org.ceteca.javaglade.toolkit.swt.SwtDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Event;

/**
 * @author Juan C. Díaz (juancarlosdiaz@ceteca.net) <br>
 *         Copyright (c) 2009 Fundación Centro Tecnolóxico da Carne
 * 
 * <br>
 * <br>
 *         This program is free software: you can redistribute it and/or modify
 *         it under the terms of the GNU General Public License as published by
 *         the Free Software Foundation, either version 3 of the License, or (at
 *         your option) any later version. <br/>
 *         This program is distributed in the hope that it will be useful, but
 *         WITHOUT ANY WARRANTY <br>
 *         without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 *         PARTICULAR PURPOSE. See the GNU General Public License for more
 *         details. <br/>
 *         You should have received a copy of the GNU General Public License
 *         along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
public class MapViewer extends GenericHandler {


	private SwtDialog dialogMap;
	private GoogleMap googleMap;
	private Canvas canvas;

	private AddressOT address;
	private CommonClient entityLauncher;
	private String mapGuiName;

	private Logger logger;

	public MapViewer() {
		super();
		// TODO Auto-generated constructor stub
	}

	public AddressOT getAddress() {
		return address;
	}

	public void setAddress(AddressOT address) {
		this.address = address;
	}

	/**
	 * Default constructor
	 * 
	 * @param address
	 * @param entityLauncher
	 */
	public MapViewer(AddressOT address, CommonClient entityLauncher, Logger log) {

		super();
		this.address = address;
		this.entityLauncher = entityLauncher;
		this.logger = log;
		
		// ojo no esta bien
		this.mapGuiName = entityLauncher.getEditionGui().getGuiName() + "\\"
				+ "Explica_MapViewer";

	}

	public void show() {
		build();
		getWidgets();
		OperationResult result = loadMap();

		if (OperationResult.RESULT_ERROR == result.getResult()) {
			OperationResult localizedResult = ClientContext.getInstance()
			.localizeOperationResult(result);
			ExplicaGUIBuilder.getInstance().showConfirmationDialog(
					this.entityLauncher.getEditionGui().getParentWindow().getShell(),
					localizedResult.getFormattedResult(), "ERROR",
					SWT.ICON_ERROR, SWT.OK);
			
		} else {
			canvas.setBackgroundImage(googleMap.getMapImage());
			canvas.redraw();
			dialogMap.open();
		}

	}

	public OperationResult loadMap() {
		OperationResult result = new OperationResult();
		googleMap=new GoogleMap(address, 13, new Dimension(canvas.getSize().x, canvas.getSize().y));
		result.add(googleMap.build());
		return result;
		
		/*
		try {
			Image image = MapUtils.getInstance().getGoogleMapImage(address,
					new Dimension(canvas.getSize().x, canvas.getSize().y), 12);
			logger.debug("Canvas Map size: " + canvas.getSize().x + "x"
					+ canvas.getSize().y);
			canvas.setBackgroundImage(image);
			canvas.redraw();
			result.addResultMessage(OperationResult.RESULT_OK, ClientContext
					.getInstance().getMsgCatalog().newResultMessage(
							InterfaceErrors.MSG_MAP_OK));
			return result;

		} catch (ExplicaException eex) {
			ResultMessage res=eex.getResMsg();
			result.addResultMessage(OperationResult.RESULT_ERROR, res);
			return result;

		}
		*/
	}

	public void close(Event e) {
		this.logger.debug("Buton Close clicked");
		dialogMap.close();
	}

	public void build() {

		try {
			this.gui = ExplicaGUIBuilder.getInstance().buildGUI(
					mapGuiName,
					InterfacePartners.EXPLICA_VIEW_MAP_GLADE_PATH,
					this.entityLauncher.getEditionGui(),
					this.entityLauncher.getEditionGui().getParentWindow());

//			this.setJavaGlade(ExplicaGUIBuilder.getInstance()
//					.getGUI(mapGuiName));

			dialogMap = this.gui.getDialog(
					InterfacePartners.EXPLICA_MAP_DIALOG_NAME);

		} catch (JavaGladeException e) {
			logger.error("Error building Map Dialog");
			e.printStackTrace();

		}

	}

	public void getWidgets() {

		canvas = (Canvas) this.gui.getWidget("drawingarea1");
		canvas.setSize(450, 450);
		canvas.setLayout(new FillLayout());

	}

}
